﻿using Appliaction.AppServices.Inv;
using Appliaction.Dto.Inv;
using Appliaction.IAppServices.Inv;
using AutoMapper;
using Domain.Entitys.Inv;
using Domain.IRepositorys.Inv;
using EFCore.Repositorys.Inv;
using Microsoft.AspNetCore.Mvc;
using static StackExchange.Redis.Role;

namespace API.Controllers.Inv
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    //[Authorize]
    public class Inv_trfController : Controller
    {
        private readonly IMapper _mapper;
        private readonly IInv_Trfservices inv_Trfservices; 

        public Inv_trfController(IInv_Trfservices inv_Trfservices, IMapper mapper)
        {
            this.inv_Trfservices = inv_Trfservices;
            _mapper=mapper;
        }

        /// <summary>
        /// 库存移转查询总数
        /// </summary>
        /// <param name="trf_code"></param>
        /// <param name="trf_type"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<int> Inv_trf_selectCount(string ? trf_code, string ? trf_type,string? proName)
        {
            return inv_Trfservices.Inv_trf_selectCount(trf_code, trf_type, proName);
        }
        /// <summary>
        /// 库存移转查询分页
        /// </summary>
        /// <param name="trf_code"></param>
        /// <param name="trf_type"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public JsonResult Inv_trf_selectFY(string ? trf_code, string ? trf_type, int currentPage, int pageSize, string? proName)
        {
            return new JsonResult(inv_Trfservices.Inv_trf_selectFY(trf_code, trf_type, currentPage, pageSize, proName));
        }


        /// <summary>
        /// 根据仓库编号查询调拨单信息
        /// </summary>
        /// <param name="trf_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async  Task<JsonResult> Inv_trf_SelectCode(string trf_code)
        {
            var result = await inv_Trfservices.Inv_trf_SelectCode(trf_code);
            return new JsonResult(result);
        }


        /// <summary>
        /// 根据编码获取调拨单详情
        /// </summary>
        /// <param name="trf_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public async Task<JsonResult> Inv_trf_SelectDetail(string trf_code)
        {
            var result = await inv_Trfservices.Inv_trf_SelectDetail(trf_code);
            return new JsonResult(result);
        }


        /// <summary>
        /// 添加库存调拨
        /// </summary>
        /// <param name="inv_TrfAddDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<int> Inv_trf_Insert(Inv_TrfAddDto inv_TrfAddDto)
        {
           return inv_Trfservices.Inv_trf_Insert(inv_TrfAddDto);
        }

        /// <summary>
        /// 根据单据编号查询库存调拨单id信息,来添加详细信息
        /// </summary>
        /// <param name="trf_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<int> inv_tfr_id(string trf_code)
        {
            return inv_Trfservices.inv_tfr_id(trf_code);
        }


        /// <summary>
        /// 添加详情表
        /// </summary>
        /// <param name="inv_TrftrfproddtlAddDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public async Task<IActionResult> Inv_trf_Inserttrfproddtl([FromBody] List<Inv_TrftrfproddtlAddDto> inv_TrftrfproddtlAddDto)
        {
            try { 
                int result = await inv_Trfservices.Inv_trf_Selecttrfproddtl(inv_TrftrfproddtlAddDto);
                return Ok(new { succes = true, count = result });

            }catch(Exception ex)
            {
                return new JsonResult(ex.Message);
            }
        }


        /// <summary>
        /// 修改调拨审核状态
        /// </summary>
        /// <param name="inv_Trf_AuditUpdadteaDto"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<int> Inv_Trf_examineUpdate(Inv_Trf_AuditUpdadteaDto inv_Trf_AuditUpdadteaDto)
        {
            return inv_Trfservices.Inv_Trf_examineUpdate(inv_Trf_AuditUpdadteaDto);
        }


        /// <summary>
        /// 删除调拨单
        /// </summary>
        /// <param name="Trf_id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<int> Inv_Trf_delete(int Trf_id)
        {
          return inv_Trfservices.Inv_Trf_delete(Trf_id);
        }


        /// <summary>
        /// 修改库存调拨单和调拨详情单和同一时间执行添加修改入库单详情
        /// </summary>
        /// <param name="inv_OthwrUpdateDto"></param>
        /// <param name="entities"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public async Task<JsonResult> Inv_othwr_Update([FromBody] Inv_TrfUpdateMerage  inv_Trf)
        {
            if (inv_Trf == null)
            {
                return new JsonResult(new { code = 0, msg = "参数为空" });
            }
            return new JsonResult(await inv_Trfservices.Inv_othwr_Update(inv_Trf));
        }



        /// <summary>
        /// 其他入库单入库查询调拨单
        /// </summary>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public  JsonResult  Inv_Select_MainSelect(string ? code,int currentPage, int pageSize)
        {
           return new JsonResult(inv_Trfservices.Inv_Select_MainSelect(code, currentPage, pageSize));
        }
        /// <summary>
        /// 其他入库单入库查询调拨单--总数
        /// </summary>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpGet]
        public Task<int> Inv_Select_MainSelect_Count(string ? code)
        {
            return inv_Trfservices.Inv_Select_MainSelect_Count(code);
        }


        /// <summary>
        /// 查询调拨单--出库单
        /// </summary>
        /// <param name="code"></param>
        /// <param name="currentPage"></param>
        /// <param name="pageSize"></param>
        /// <returns></returns>
        [HttpGet]
        public JsonResult Inv_Select_othoutordSelect(string ? code, int currentPage, int pageSize)
        {

          return new JsonResult(inv_Trfservices.Inv_Select_othoutordSelect(code, currentPage, pageSize));
        }

        /// <summary>
        /// 其他出库单入库查询调拨单
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Task<int> Inv_Select_othoutordSelect_Count(string ? code)
        {

            return inv_Trfservices.Inv_Select_othoutordSelect_Count(code);
        }



        ///// <summary>
        ///// 回调库存调拨单的数量--其他入库单审核入库之后,减少调拨单的数量
        ///// </summary>
        ///// <param name="id"></param>
        ///// <returns></returns>
        //[HttpPut]
        //public async  Task<IActionResult> Inv_Trf_update_numderl(List<Inv_TrfupdateNumderDto> inv_TrfupdateNumderDto)
        //{
        //    int result = await inv_Trfservices.Inv_Trf_update_numderl(inv_TrfupdateNumderDto);
        //    return Ok(new { succes = true, count = result });
        //}


        /// <summary>
        /// 根据其他出库单查询的Id,查询出来关联单号,去修改调拨单的字段状态
        /// </summary>
        /// <param name="othoutord_id"></param>
        /// <returns></returns>
        [HttpPut]
        public Task<int> Inv_Trf_update_trf_ckpd(int othoutord_id)
        {

            return inv_Trfservices.Inv_Trf_update_trf_ckpd(othoutord_id);
        }


        /// <summary>
        /// 入库完善审核后,修改调拨单的待入库数量
        /// </summary>
        /// <param name="inv_Othwrproddtls"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<int> Inv_Trf_update_Pending_numder(List<Inv_Trf_update_Pending_numderUpdateDto> inv_Trf_update_Pending_numderUpdateDto)
        {
            int result = await inv_Trfservices.Inv_Trf_update_Pending_numder(inv_Trf_update_Pending_numderUpdateDto);

            return result;
        }

    }
}
